/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xib.android;

import android.graphics.Bitmap;
import java.io.IOException;
import java.util.ArrayList;
import xib.Page;
import xib.Panel;
import xib.core.XibController;

/**
 *
 * @author xavierds
 */
public final class HandlerAndroid implements IHandlerAndroid{

    private XibController xibController;
    private ArrayList<String> availablesLanguages;
    private String currentLanguage;


    public HandlerAndroid(){
        xibController = null;
    }

    public void loadFile(String pathname) throws IOException {
        xibController = new XibController();
        xibController.loadFile(pathname);
        availablesLanguages =  xibController.getAvailablesLanguages();
        currentLanguage = xibController.getDefaultLanguage();
    }

    public ArrayList<Integer> getChaptersNumbers() {
        isInitialized();
        return xibController.getChaptersNumbers();
    }

    public int getTotalPages() {
        isInitialized();
        return xibController.getTotalPages();
    }

    public int getTotalPanels(){
        isInitialized();
        return xibController.getTotalPanels();
    }

    public int getNumPagesOfChapter(int chapterNumber) {
        isInitialized();
        return xibController.getNumPagesOfChapter(chapterNumber);
    }

    public ArrayList<String> getAvailaiblesLanguages() {
        isInitialized();
        return availablesLanguages;
    }

    public boolean setLanguage(String language) {
        isInitialized();
        boolean res = false;
        if(availablesLanguages.contains(language)){
            currentLanguage = language;
            res = true;
        }
        return res;
    }
    
    public Page getNextPage() {
        isInitialized();
        return xibController.getNextPage();
    }


    public Page getPreviousPage() {
        isInitialized();
        return xibController.getPreviousPage();
    }


    public Page getPageAt(int chapterNumber, int page) {
        isInitialized();
        return xibController.getPageAt(chapterNumber, page);
    }

    public Page getPageAt(int page) {
        isInitialized();
        return xibController.getPageAt(page);
    }

    public Bitmap getPageBitmap(Page page) {
        isInitialized();
        return BitmapBuilder.getPage(currentLanguage, page);
    }


    public Panel getNextPanel() {
        isInitialized();
        return xibController.getNextPanel();
    }

    public Panel getPreviousPanel() {
        isInitialized();
        return xibController.getPreviousPanel();
    }

    public Panel getPanelAt(int chapterNumber, int page, int panelOrder) {
        isInitialized();
        return xibController.getPanelAt(chapterNumber, page, panelOrder);
    }

    public Panel getPanelAt(int page, int panelOrder) {
        isInitialized();
        return xibController.getPanelAt(page, panelOrder);
    }

    public Bitmap getPanelBitmap(Panel panel) {
        return BitmapBuilder.getPanel(currentLanguage, panel);
    }

    private void isInitialized(){
        if(xibController == null)
            throw new RuntimeException();
    }

    
}
